// pages/news/news.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    news: [],
    page: 1,
    readPercent:0
  },
  tonewsDetail: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/dynamicLink/dynamicLink?article_id='+ id,
    })
  },
  // 滚动计算阅读
  scroll: function (e) {
    var that = this;
    var scrollHeight = e.detail.scrollHeight;
    var scrollTop = e.detail.scrollTop;
    var windowHeight = that.data.windowHeight;
    var height = scrollHeight - windowHeight - 20;
    var scrollPercent = (scrollTop / height) * 100;
    scrollPercent = Math.ceil(scrollPercent);
    var readPercent = that.data.readPercent;
    if (scrollPercent > 100) {
      scrollPercent = 100;
      readPercent = 100;
    } else {
      scrollPercent = scrollPercent;
      if (scrollPercent > readPercent) {
        readPercent = scrollPercent;
      } else {
        readPercent = readPercent;
      }
    }
    that.setData({
      readPercent: readPercent
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    app.httpRequest('article.article/lists', {}, 'get', false, function(res){
      that.setData({
        lists: res.data.lists.data,
        pageShow: true,
        page: ++that.data.page,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var startTime = new Date().getTime();
    that.setData({
      startTime: startTime,
      windowHeight: wx.getStorageSync('systemInfo').windowHeight,
    })
  },
  // 获得停留时间，毫秒级
  getStopTime: function () {
    var that = this;
    var endTime = new Date().getTime();
    var startTime = that.data.startTime;
    var stopTime = endTime - startTime;
    that.setData({
      stopTime: stopTime
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(127,'查看了资讯列表',stoptime,percent)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    that.getStopTime();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    if(page < 0) return;
    app.httpRequest('article.article/lists', {page: page}, 'get', false, function(res){
      if(res.data.lists.data.length < 1){
        that.setData({
          page: -1,
        })
      }else{
        that.setData({
          lists: that.data.lists.concat(res.data.lists.data),
          page: ++page,
        })
      }
     
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.userBehavior(128,'分享资讯列表')
  }
})